import { url_role } from "@/comm/api";
import { get, patch, post, remove } from "@/comm/request";
import { defineStore } from "pinia";

interface Role {
    _id: string;
    name: string;
    description?: string;
    permissions: string[];
}

interface IState {
    curRole: Role;
    list: Role[];
}

export const useRoleStore = defineStore({
    id: "role",
    state: (): IState => ({
        curRole: null as any,
        list: []
    }),
    actions: {
        async load() {
            let res: any = await get(url_role);
            console.log("res", res)
            this.list = res.data;
        },
        async add(data: any) {
            let { _id, ...param } = data;
            console.log("param", param)
            let res: any = await post(url_role, param, true);
            console.log("res", res)
            // this.list = res.data;
        },
        async update(data: any) {
            let { _id, ...param } = data;
            console.log("param", param)
            let res: any = await patch(url_role + "/" + _id, param, true);
            console.log("res", res)
            // this.list = res.data;
        },
        async remove(id: string) {
            let res: any = await remove(url_role + "/" + id);
            console.log("res", res)
            // this.list = res.data;
        }

    }
});
